import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;
import org.w3c.dom.*;

public class XmlOperations
{
	private static XmlOperations xo=null;

	private XmlOperations(){}
	
	public static XmlOperations getInstance()
	{
		System.gc();
		if(xo==null)
		{
			xo=new XmlOperations();
		}
		
		return xo;
	}
	
	//Batch Operations
	public ProgramUitl.ERROR_TYPE addBatch(Batch b)
	{
		if(isFileAvailable(ProgramUitl.BATCH_XML))
		{
			if(searchBatchByNo(Integer.toString(b.getBatchCode()))!=null)
				return ProgramUitl.ERROR_TYPE.ALREADY_EXISTS;				
		}
		else
		{		
			createXml(ProgramUitl.BATCH_XML,ProgramUitl.BATCH_XML_ELEMENT_ROOT);
		}
				
		try
		{	
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			Document document = documentBuilder.parse(ProgramUitl.BATCH_XML);
			Element root = document.getDocumentElement();
			Element rootElement = document.getDocumentElement();
			
			Element batch = document.createElement(ProgramUitl.BATCH_XML_ELEMENT);	
			
			batch.appendChild(getElement(document, batch, ProgramUitl.BATCH_XML_NO_ELEMENT, Integer.toString(b.getBatchCode()),false));
			batch.appendChild(getElement(document, batch, ProgramUitl.BATCH_XML_DESC_ELEMENT, b.getBatchDesc(),true));
		
			batch.appendChild(getElement(document, batch, ProgramUitl.XML_STATUS_ELEMENT, ProgramUitl.getStatusString(b.getStatus()),false));
			
			rootElement.appendChild(batch);
			
			DOMSource source = new DOMSource(document);

			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			StreamResult result = new StreamResult(ProgramUitl.BATCH_XML);
			transformer.transform(source, result);		
		} 
		catch (Throwable t) { 
			throw new Error(t);
		} 
		return ProgramUitl.ERROR_TYPE.ADDED;				
	}
	
	public Batch searchBatchByNo(String batchNo)
	{
		return searchBatchByNo(ProgramUitl.BATCH_XML,batchNo);
	}

	public Batch searchBatchByNo(String fileName, String batchNo)
	{
		if(isFileAvailable(fileName))
		{
			try
			{		
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document document = db.parse(fileName);
				NodeList batchs = document.getElementsByTagName(ProgramUitl.BATCH_XML_ELEMENT);

				if (batchs.getLength() > 0)
				{	
					
					for (int i = 0; i < batchs.getLength(); i++) 
					{
						Node node = batchs.item(i);
						if (node.getNodeType() == Node.ELEMENT_NODE) 
						{
							
							Element element = (Element) node;							
							if(element.getElementsByTagName(ProgramUitl.BATCH_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue().toLowerCase().equals(batchNo.toLowerCase()))
							{							
								Batch b=new Batch();
								b.setBatchCode(Integer.parseInt(element.getElementsByTagName(ProgramUitl.BATCH_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								b.setBatchDesc(element.getElementsByTagName(ProgramUitl.BATCH_XML_DESC_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								b.setStatus(ProgramUitl.getStatusEnum(element.getElementsByTagName(ProgramUitl.XML_STATUS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								return b;
							}
						}
					}				
				}
				return null;
			} 
			catch (Throwable t) 
			{ 
				throw new Error(t);
			} 
		}
		else
		{
			return null;
		}
	}
	
	public List<Batch> getAllBatches()
	{
		return getAllBatches(ProgramUitl.BATCH_XML);
	}
	
	public List<Batch> getAllBatches(String fileName)
	{
		if(isFileAvailable(fileName))
		{
			try
			{		
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document document = db.parse(fileName);
				NodeList batches = document.getElementsByTagName(ProgramUitl.BATCH_XML_ELEMENT);
				List<Batch> batchList=null;
				if (batches.getLength() > 0)
				{			
					batchList=new ArrayList<Batch>();
					
					for (int i = 0; i < batches.getLength(); i++) 
					{			
						Node node = batches.item(i);
						if (node.getNodeType() == Node.ELEMENT_NODE) 
						{
							Element element = (Element) node;																			
							if(ProgramUitl.getStatusEnum(element.getElementsByTagName(ProgramUitl.XML_STATUS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()).equals(ProgramUitl.STATUS.ENABLED))
							{							
								Batch b=new Batch();
								b.setBatchCode(Integer.parseInt(element.getElementsByTagName(ProgramUitl.BATCH_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								b.setBatchDesc(element.getElementsByTagName(ProgramUitl.BATCH_XML_DESC_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								b.setStatus(ProgramUitl.getStatusEnum(element.getElementsByTagName(ProgramUitl.XML_STATUS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								
								batchList.add(b);
							}
						}
					}				
				}
				return batchList;
			} 
			catch (Throwable t) 
			{ 
				throw new Error(t);
			}
		}
		else
		{
			return null;
		}		
	}	//
	
	//Student Operations
	public ProgramUitl.ERROR_TYPE addStudent(Student s)
	{
		if(isFileAvailable(ProgramUitl.STUDENT_XML))
		{
			if(searchStudentById(ProgramUitl.STUDENT_XML,s.getIdNo())!=null)
				return ProgramUitl.ERROR_TYPE.ALREADY_EXISTS;				
		}
		else
		{		
			createXml(ProgramUitl.STUDENT_XML,ProgramUitl.STUDENT_XML_ELEMENT_ROOT);
		}
				
		try
		{	
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			Document document = documentBuilder.parse(ProgramUitl.STUDENT_XML);
			Element root = document.getDocumentElement();
			Element rootElement = document.getDocumentElement();
			
			Element student = document.createElement(ProgramUitl.STUDENT_XML_ELEMENT);	
			
			student.appendChild(getElement(document, student, ProgramUitl.STUDENT_XML_NO_ELEMENT, Integer.toString(s.getNo()),false));
			student.appendChild(getElement(document, student, ProgramUitl.STUDENT_XML_ID_ELEMENT, s.getIdNo(),false));
			student.appendChild(getElement(document, student, ProgramUitl.STUDENT_XML_NAME_ELEMENT, s.getName() ,false));
			student.appendChild(getElement(document, student, ProgramUitl.STUDENT_XML_AGE_ELEMENT, Integer.toString(s.getAge()),false));
			student.appendChild(getElement(document, student, ProgramUitl.STUDENT_XML_ADDRESS_ELEMENT, s.getAddress(),true));
			student.appendChild(getElement(document, student, ProgramUitl.STUDENT_XML_TP_ELEMENT, s.getTel(),false));			
			student.appendChild(getElement(document, student, ProgramUitl.STUDENT_XML_BATCH_ELEMENT, Integer.toString(s.getBatchCode()),false));
			student.appendChild(getElement(document, student, ProgramUitl.XML_STATUS_ELEMENT, ProgramUitl.getStatusString(s.getStatus()),false));
			
			rootElement.appendChild(student);
			
			DOMSource source = new DOMSource(document);

			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			StreamResult result = new StreamResult(ProgramUitl.STUDENT_XML);
			transformer.transform(source, result);		
		} 
		catch (Throwable t) { 
			throw new Error(t);
		} 
		return ProgramUitl.ERROR_TYPE.ADDED;				
	}

	public Student searchStudentByNo(String studentNo)
	{		
		return searchStudentByNo(ProgramUitl.STUDENT_XML,studentNo);
	}	
	
	public Student searchStudentByNo(String fileName, String studentNo)
	{
		if(isFileAvailable(fileName))
		{
			try
			{		
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document document = db.parse(fileName);
				NodeList students = document.getElementsByTagName(ProgramUitl.STUDENT_XML_ELEMENT);

				if (students.getLength() > 0)
				{	
					
					for (int i = 0; i < students.getLength(); i++) 
					{
						Node node = students.item(i);
						if (node.getNodeType() == Node.ELEMENT_NODE) 
						{
							
							Element element = (Element) node;							
							if(element.getElementsByTagName(ProgramUitl.STUDENT_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue().toLowerCase().equals(studentNo.toLowerCase()))
							{							
								Student s=new Student();
								s.setNo(Integer.parseInt(element.getElementsByTagName(ProgramUitl.STUDENT_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								s.setIdNo(element.getElementsByTagName(ProgramUitl.STUDENT_XML_ID_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								s.setName(element.getElementsByTagName(ProgramUitl.STUDENT_XML_NAME_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								s.setAge(Integer.parseInt(element.getElementsByTagName(ProgramUitl.STUDENT_XML_AGE_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								s.setAddress(element.getElementsByTagName(ProgramUitl.STUDENT_XML_ADDRESS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								s.setTel(element.getElementsByTagName(ProgramUitl.STUDENT_XML_TP_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								s.setBatchCode(Integer.parseInt(element.getElementsByTagName(ProgramUitl.STUDENT_XML_BATCH_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								s.setStatus(ProgramUitl.getStatusEnum(element.getElementsByTagName(ProgramUitl.XML_STATUS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								return s;
							}
						}
					}				
				}
				return null;
			} 
			catch (Throwable t) 
			{ 
				throw new Error(t);
			} 
		}
		else
		{
			return null;
		}
	}
	
	public Student searchStudentById(String studentId)
	{
		return searchStudentById(ProgramUitl.STUDENT_XML,studentId);
	}
	
	public Student searchStudentById(String fileName, String studentId)
	{
		if(isFileAvailable(fileName))
		{	
			try
			{		
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document document = db.parse(fileName);
				NodeList students = document.getElementsByTagName(ProgramUitl.STUDENT_XML_ELEMENT);

				if (students.getLength() > 0)
				{	
					
					for (int i = 0; i < students.getLength(); i++) 
					{
						Node node = students.item(i);
						if (node.getNodeType() == Node.ELEMENT_NODE) 
						{
							
							Element element = (Element) node;							
							if(element.getElementsByTagName(ProgramUitl.STUDENT_XML_ID_ELEMENT).item(0).getChildNodes().item(0).getNodeValue().toLowerCase().equals(studentId.toLowerCase()))
							{							
								Student s=new Student();
								s.setNo(Integer.parseInt(element.getElementsByTagName(ProgramUitl.STUDENT_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								s.setIdNo(element.getElementsByTagName(ProgramUitl.STUDENT_XML_ID_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								s.setName(element.getElementsByTagName(ProgramUitl.STUDENT_XML_NAME_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								s.setAge(Integer.parseInt(element.getElementsByTagName(ProgramUitl.STUDENT_XML_AGE_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								s.setAddress(element.getElementsByTagName(ProgramUitl.STUDENT_XML_ADDRESS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								s.setTel(element.getElementsByTagName(ProgramUitl.STUDENT_XML_TP_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								s.setBatchCode(Integer.parseInt(element.getElementsByTagName(ProgramUitl.STUDENT_XML_BATCH_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								s.setStatus(ProgramUitl.getStatusEnum(element.getElementsByTagName(ProgramUitl.XML_STATUS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								return s;
							}
						}
					}				
				}
				return null;
			} 
			catch (Throwable t) 
			{ 
				throw new Error(t);
			} 
		}
		else
		{
			return null;
		}		
	}
	
	public static int getStudentNo()
	{
		return getLastStudentNo(ProgramUitl.STUDENT_XML)+1;
	}
	public static int getLastStudentNo(String fileName)
	{
		int lastIndex=0;		
		try
		{		
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.parse(fileName);
			NodeList students = document.getElementsByTagName(ProgramUitl.STUDENT_XML_ELEMENT);
			
			if (students.getLength() > 0)
			{					
				for (int i = 0; i < students.getLength(); i++) 
				{
					Node node = students.item(i);
					if (node.getNodeType() == Node.ELEMENT_NODE) 
					{
						Element element = (Element) node;
						lastIndex=Integer.parseInt(element.getElementsByTagName(ProgramUitl.STUDENT_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
					}
				}				
			}
			return lastIndex;
		} 
		catch (Exception ex) 
		{ 						
			return lastIndex;
		} 	
	}
	
	public List<Student> getStudenByBatch(String batchNo)
	{
		return getStudenByBatch(ProgramUitl.STUDENT_XML, batchNo);
	}
	public List<Student> getStudenByBatch(String fileName, String batchNo)
	{
		
		if(isFileAvailable(fileName))
		{
		
			try
			{		
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document document = db.parse(fileName);
				NodeList students = document.getElementsByTagName(ProgramUitl.STUDENT_XML_ELEMENT);
				List<Student> studentList=null;
				if (students.getLength() > 0)
				{	
					
					studentList=new ArrayList<Student>();
					
					for (int i = 0; i < students.getLength(); i++) 
					{			
						Node node = students.item(i);
						if (node.getNodeType() == Node.ELEMENT_NODE) 
						{
							Element element = (Element) node;
							//System.out.println(batchNo.trim());
							
							if(element.getElementsByTagName(ProgramUitl.STUDENT_XML_BATCH_ELEMENT).item(0).getChildNodes().item(0).getNodeValue().equals(batchNo.trim()))
							{
								if(ProgramUitl.getStatusEnum(element.getElementsByTagName(ProgramUitl.XML_STATUS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()).equals(ProgramUitl.STATUS.ENABLED))
								{															
									Student s=new Student();
									s.setNo(Integer.parseInt(element.getElementsByTagName(ProgramUitl.STUDENT_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
									s.setIdNo(element.getElementsByTagName(ProgramUitl.STUDENT_XML_ID_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
									s.setName(element.getElementsByTagName(ProgramUitl.STUDENT_XML_NAME_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
									s.setAge(Integer.parseInt(element.getElementsByTagName(ProgramUitl.STUDENT_XML_AGE_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
									s.setAddress(element.getElementsByTagName(ProgramUitl.STUDENT_XML_ADDRESS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
									s.setTel(element.getElementsByTagName(ProgramUitl.STUDENT_XML_TP_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
									s.setBatchCode(Integer.parseInt(element.getElementsByTagName(ProgramUitl.STUDENT_XML_BATCH_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
									s.setStatus(ProgramUitl.getStatusEnum(element.getElementsByTagName(ProgramUitl.XML_STATUS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
									
									studentList.add(s);
								}
							}
						}
					}				
				}
				return studentList;
			} 
			catch (Throwable t) 
			{ 
				throw new Error(t);
			}
		}
		else
		{
			return null;
		}		
	}
	//
	
	//Course Operations
	public ProgramUitl.ERROR_TYPE addCourse(Course c)
	{
		if(isFileAvailable(ProgramUitl.COURSE_XML))
		{
			if(searchCourseByNo(ProgramUitl.COURSE_XML,Integer.toString(c.getCode()))!=null)
				return ProgramUitl.ERROR_TYPE.ALREADY_EXISTS;				
		}
		else
		{		
			createXml(ProgramUitl.COURSE_XML,ProgramUitl.COURSE_XML_ELEMENT_ROOT);
		}
				
		try
		{	
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			Document document = documentBuilder.parse(ProgramUitl.COURSE_XML);
			Element root = document.getDocumentElement();
			Element rootElement = document.getDocumentElement();
			
			Element course = document.createElement(ProgramUitl.COURSE_XML_ELEMENT);	
			
			course.appendChild(getElement(document, course, ProgramUitl.COURSE_XML_NO_ELEMENT, Integer.toString(c.getCode()),false));
			course.appendChild(getElement(document, course, ProgramUitl.COURSE_XML_NAME_ELEMENT, c.getName(),true));
			course.appendChild(getElement(document, course, ProgramUitl.COURSE_XML_TYPE_ELEMENT, ProgramUitl.getCourseTypeString(c.getType()) ,false));
			course.appendChild(getElement(document, course, ProgramUitl.COURSE_XML_CREDITS_ELEMENT, Integer.toString(c.getCredits()),false));
			
			if(c.getType()==ProgramUitl.COURSE_TYPE.PROJECT)
			{
				course.appendChild(getElement(document, course, ProgramUitl.COURSE_XML_HASPRACTICAL_ELEMENT, ProgramUitl.booleanToString(false),false));		
			}
			else
			{
				course.appendChild(getElement(document, course, ProgramUitl.COURSE_XML_HASPRACTICAL_ELEMENT, ProgramUitl.booleanToString(c.getHasPracticals()),false));		
			}
			
			course.appendChild(getElement(document, course, ProgramUitl.XML_STATUS_ELEMENT, ProgramUitl.getStatusString(c.getStatus()),false));
			
			rootElement.appendChild(course);
			
			DOMSource source = new DOMSource(document);

			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			StreamResult result = new StreamResult(ProgramUitl.COURSE_XML);
			transformer.transform(source, result);		
		} 
		catch (Throwable t) { 
			throw new Error(t);
		} 
		return ProgramUitl.ERROR_TYPE.ADDED;				
	}
	
	public Course searchCourseByNo(String courseNo)
	{
		return searchCourseByNo(ProgramUitl.COURSE_XML, courseNo);
	}
	public Course searchCourseByNo(String fileName, String courseNo)
	{
		if(isFileAvailable(fileName))
		{
			try
			{		
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document document = db.parse(fileName);
				NodeList courses = document.getElementsByTagName(ProgramUitl.COURSE_XML_ELEMENT);

				if (courses.getLength() > 0)
				{	
					
					for (int i = 0; i < courses.getLength(); i++) 
					{
						Node node = courses.item(i);
						if (node.getNodeType() == Node.ELEMENT_NODE) 
						{
							
							Element element = (Element) node;							
							if(element.getElementsByTagName(ProgramUitl.COURSE_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue().toLowerCase().equals(courseNo.toLowerCase()))
							{							
								Course c=new Course();
								c.setCode(Integer.parseInt(element.getElementsByTagName(ProgramUitl.COURSE_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								c.setName(element.getElementsByTagName(ProgramUitl.COURSE_XML_NAME_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								c.setType(ProgramUitl.getCourseTypeEnum(element.getElementsByTagName(ProgramUitl.COURSE_XML_TYPE_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								c.setCredits(Integer.parseInt(element.getElementsByTagName(ProgramUitl.COURSE_XML_CREDITS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								c.setHasPracticals(ProgramUitl.stringToBoolean(element.getElementsByTagName(ProgramUitl.COURSE_XML_HASPRACTICAL_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								c.setStatus(ProgramUitl.getStatusEnum(element.getElementsByTagName(ProgramUitl.XML_STATUS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								return c;
							}
						}
					}				
				}
				return null;
			} 
			catch (Throwable t) 
			{ 
				throw new Error(t);
			} 
		}	
		else		
		{
			return null;
		}
	}
	
	public List<Course> getAllCourse()
	{
		return getAllCourse(ProgramUitl.COURSE_XML);
	}
	
	public List<Course> getAllCourse(String fileName)
	{
		if(isFileAvailable(fileName))
		{
			try
			{		
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document document = db.parse(fileName);
				NodeList courses = document.getElementsByTagName(ProgramUitl.COURSE_XML_ELEMENT);
				List<Course> courseList=null;
				if (courses.getLength() > 0)
				{			
					courseList=new ArrayList<Course>();
					
					for (int i = 0; i < courses.getLength(); i++) 
					{			
						Node node = courses.item(i);
						if (node.getNodeType() == Node.ELEMENT_NODE) 
						{
							Element element = (Element) node;																			
							if(ProgramUitl.getStatusEnum(element.getElementsByTagName(ProgramUitl.XML_STATUS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()).equals(ProgramUitl.STATUS.ENABLED))
							{							
								Course c=new Course();
								c.setCode(Integer.parseInt(element.getElementsByTagName(ProgramUitl.COURSE_XML_NO_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								c.setName(element.getElementsByTagName(ProgramUitl.COURSE_XML_NAME_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
								c.setType(ProgramUitl.getCourseTypeEnum(element.getElementsByTagName(ProgramUitl.COURSE_XML_TYPE_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								c.setCredits(Integer.parseInt(element.getElementsByTagName(ProgramUitl.COURSE_XML_CREDITS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								c.setHasPracticals(ProgramUitl.stringToBoolean(element.getElementsByTagName(ProgramUitl.COURSE_XML_HASPRACTICAL_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
								c.setStatus(ProgramUitl.getStatusEnum(element.getElementsByTagName(ProgramUitl.XML_STATUS_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));	
								
								courseList.add(c);
							}
						}
					}				
				}
				return courseList;
			} 
			catch (Throwable t) 
			{ 
				throw new Error(t);
			}
		}
		else
		{
			return null;
		}		
	}	
	//
	
	//Enrollment Operations
	public List<Enroll> getStudentEnrolInfo(String studentNo)
	{		
		//return  getStudentEnrolInfo(ProgramUitl.ENROLEMENET_DB_PATH+studentNo+ProgramUitl.XML, studentNo);
		return  getStudentEnrolInfo(ProgramUitl.ENROLEMENET_DB_PATH+studentNo, studentNo);
	}
	
	public List<Enroll> getStudentEnrolInfo(String fileName,String studentNo)
	{
		List<Enroll> enrollList=null;
		
		if(isFolderAvailable(fileName))
		{
			try
			{		
				List<String> fileList=getAllFiles(fileName,true);
				
				if(fileList!=null )
				{
					if (fileList.size() > 0)
					{							
						enrollList=new ArrayList<Enroll>();
						
						for (int i = 0; i < fileList.size(); i++) 
						{																
							Enroll e=new Enroll();
							e.setNo(Integer.parseInt(studentNo));
							e.setCode(Integer.parseInt(fileList.get(i)));		
							enrollList.add(readEnrollBody(ProgramUitl.ENROLEMENET_DB_PATH+studentNo+ProgramUitl.DIR_SEP+fileList.get(i)+ProgramUitl.XML,e));							
							
						}				
					}
				}
				return enrollList;
			} 
			catch (Throwable t) 
			{ 
				throw new Error(t);
			} 
		}
		
		return enrollList;
	}
	
	
	public Enroll getStudentEnrolInfoByStNoCoId(String studentNo,String courseNo)
	{
		String fileName=ProgramUitl.ENROLEMENET_DB_PATH+studentNo+ProgramUitl.DIR_SEP+courseNo+ProgramUitl.XML;		
		if(isFileAvailable(fileName))
		{			
			Enroll e=new Enroll();
			e.setNo(Integer.parseInt(studentNo));
			e.setCode(Integer.parseInt(courseNo));
			return readEnrollBody(fileName,e);
		}
		else
		{		
			return null;
		}
	}
	
	public Enroll readEnrollBody(String fileName,Enroll e)
	{
		
		try
		{		
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.parse(fileName);
			//NodeList students = document.getElementsByTagName(ProgramUitl.STUDENT_XML_ELEMENT);
				
			//Core/Compulsory/Optional	
			e.setMarksAssignment1(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_ASSIGNMENT_1_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksAssignment2(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_ASSIGNMENT_2_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksAssignment3(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_ASSIGNMENT_3_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksClassTest(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_CLASS_TEST_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksPractical(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PRCTICAL_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksFinalPaper(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_FINAL_PAPER_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			
			//Repeat
			e.setMarksAssignment1Repeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_ASSIGNMENT_1_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksAssignment2Repeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_ASSIGNMENT_2_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksAssignment3Repeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_ASSIGNMENT_3_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksClassTestRepeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_CLASS_TEST_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksPracticalRepeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PRCTICAL_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksFinalPaperRepeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_FINAL_PAPER_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));			
			//
			//
			
			//Project
			e.setMarksInterimReport(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PROJECT_INTERIM_REPORT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksInterimPresentation(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PROJECT_INTERIM_PRESENTATION_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksFinalReport(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_REPORT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksFinalPresentation(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_PRESENTATION_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksFinalProduct(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_PRODUCT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			
			//Repeat
			e.setMarksInterimReportRepeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PROJECT_INTERIM_REPORT_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksInterimPresentationRepeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PROJECT_INTERIM_PRESENTATION_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksFinalReportRepeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_REPORT_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksFinalPresentationRepeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_PRESENTATION_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setMarksFinalProductRepeat(Integer.parseInt(document.getElementsByTagName(ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_PRODUCT_REPEAT_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));			
			//			
			//
			e.setFinalMark(Float.parseFloat(document.getElementsByTagName(ProgramUitl.ENROLL_FINAL_MARK_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
			e.setFGrade(document.getElementsByTagName(ProgramUitl.ENROLL_FINAL_GRADE_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue());
			e.setFinalGP(Float.parseFloat(document.getElementsByTagName(ProgramUitl.ENROLL_FINAL_GP_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));	
			
			return e;
		} 
		catch (Throwable t) 
		{ 
			throw new Error(t);
		} 
	}

	public boolean updateEnroll(Enroll e)
	{
		try
		{
			String fileName=ProgramUitl.ENROLEMENET_DB_PATH+e.getNo()+ProgramUitl.DIR_SEP+e.getCode()+ProgramUitl.XML;
			deleteXml(fileName);
			createXml(fileName,ProgramUitl.ENROLL_XML_ELEMENT);
			createEnrollFileBody(fileName, e);
		} 
		catch (Throwable t) { 
			throw new Error(t);
		} 		
		return true;
	}
	
	public boolean createEnrollFileBody(String fileName, Enroll e)
	{
		
		try
		{	
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			Document document = documentBuilder.parse(fileName);
			Element root = document.getDocumentElement();
			//Element rootElement = document.getDocumentElement();
			
			//Normal		
			
			//Core/Compulsory/Optional
			
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_ASSIGNMENT_1_XML_ELEMENT, Integer.toString(e.getMarksAssignment1()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_ASSIGNMENT_2_XML_ELEMENT, Integer.toString(e.getMarksAssignment2()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_ASSIGNMENT_3_XML_ELEMENT, Integer.toString(e.getMarksAssignment3()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_CLASS_TEST_XML_ELEMENT, Integer.toString(e.getMarksClassTest()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PRCTICAL_XML_ELEMENT, Integer.toString(e.getMarksPractical()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_FINAL_PAPER_XML_ELEMENT, Integer.toString(e.getMarksFinalPaper()),false));
			//System.out.println("//Core/Compulsory/Optional");
			//
			//Project
			
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PROJECT_INTERIM_REPORT_XML_ELEMENT, Integer.toString(e.getMarksInterimReport()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PROJECT_INTERIM_PRESENTATION_XML_ELEMENT, Integer.toString(e.getMarksInterimPresentation()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_REPORT_XML_ELEMENT, Integer.toString(e.getMarksFinalReport()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_PRESENTATION_XML_ELEMENT, Integer.toString(e.getMarksFinalPresentation()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_PRODUCT_XML_ELEMENT, Integer.toString(e.getMarksFinalProduct()),false));						
			//
						
			//rootElement.appendChild(normal);
			//
			
			//Repeat			
			//Core/Compulsory/Optional
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_ASSIGNMENT_1_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksAssignment1Repeat()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_ASSIGNMENT_2_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksAssignment2Repeat()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_ASSIGNMENT_3_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksAssignment3Repeat()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_CLASS_TEST_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksClassTestRepeat()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PRCTICAL_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksPracticalRepeat()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_FINAL_PAPER_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksFinalPaperRepeat()),false));			
			//
			//Project
			
			//System.out.println("//Project R");
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PROJECT_INTERIM_REPORT_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksInterimReportRepeat()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PROJECT_INTERIM_PRESENTATION_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksInterimPresentationRepeat()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_REPORT_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksFinalReportRepeat()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_PRESENTATION_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksFinalPresentationRepeat()),false));
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_MARKS_PROJECT_FINAL_PRODUCT_REPEAT_XML_ELEMENT, Integer.toString(e.getMarksFinalProductRepeat()),false));			
			//
			//Final Grade,marks,GP

			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_FINAL_MARK_XML_ELEMENT, Float.toString(e.getFinalMark()),false));
			if( e.getFGrade()==null)
			{
				root.appendChild(getElement(document, root, ProgramUitl.ENROLL_FINAL_GRADE_XML_ELEMENT, ProgramUitl.GRADE_DEFAULT,false));
			}		
			else
			{
				root.appendChild(getElement(document, root, ProgramUitl.ENROLL_FINAL_GRADE_XML_ELEMENT, e.getFGrade(),false));
			}
			root.appendChild(getElement(document, root, ProgramUitl.ENROLL_FINAL_GP_XML_ELEMENT, Float.toString(e.getFinalGP()),false));
			//
			
			DOMSource source = new DOMSource(document);			
			TransformerFactory transformerFactory = TransformerFactory.newInstance();			
			Transformer transformer = transformerFactory.newTransformer();					
			StreamResult result = new StreamResult(fileName);		
			transformer.transform(source, result);		
			
		} 
		catch (Throwable t) { 
			throw new Error(t);
		}
			
		return true;
	}
	
	public boolean createEnrollBatchFileBody(String fileName, List<Enroll> e)
	{
		
		try
		{	
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			Document document = documentBuilder.parse(fileName);
			Element root = document.getDocumentElement();
			Element rootElement = document.getDocumentElement();
			
			for (int i = 0; i < e.size(); i++) 
			{
				Enroll eTmp=(Enroll)e.get(i);
				Element enroll = document.createElement(ProgramUitl.ENROLL_XML_ELEMENT);	
				enroll.appendChild(getElement(document, enroll, ProgramUitl.ENROLL_COURSE_NO_XML_ELEMENT, Integer.toString(eTmp.getCode()),false));	
				//enroll.appendChild(getElement(document, enroll, ProgramUitl.XML_STATUS_ELEMENT, ProgramUitl.getStatusString(e.getStatus()),false));
				rootElement.appendChild(enroll);				
			}
			
			DOMSource source = new DOMSource(document);

			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			StreamResult result = new StreamResult(fileName);
			transformer.transform(source, result);	
			
		} 
		catch (Throwable t) { 
			throw new Error(t);
		} 	
		return true;			
	}
		
	public List<Enroll> getBatchEnrolInfo(String batchNo)
	{				
		return  getBatchEnrolInfo(ProgramUitl.BATCH_DB_PATH+batchNo+ProgramUitl.XML, batchNo);
	}
	
	public List<Enroll> getBatchEnrolInfo(String fileName,String batchNo)
	{
		List<Enroll> enrollList=null;
		
		if(isFileAvailable(fileName))
		{
			try
			{		
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document document = db.parse(fileName);
				NodeList enrollments = document.getElementsByTagName(ProgramUitl.ENROLL_XML_ELEMENT);				
				if (enrollments.getLength() > 0)
				{			
					enrollList=new ArrayList<Enroll>();
					
					for (int i = 0; i < enrollments.getLength(); i++) 
					{			
						Node node = enrollments.item(i);
						if (node.getNodeType() == Node.ELEMENT_NODE) 
						{
							Element element = (Element) node;																			
													
							Enroll s=new Enroll();
							s.setCode(Integer.parseInt(element.getElementsByTagName(ProgramUitl.ENROLL_COURSE_NO_XML_ELEMENT).item(0).getChildNodes().item(0).getNodeValue()));
							enrollList.add(s);

						}
					}				
				}				
				

				return enrollList;
			} 
			catch (Throwable t) 
			{ 
				throw new Error(t);
			} 
		}
		
		return enrollList;
	}
	
	public List<Enroll> getStudentEnrolInfoByCourseType(String studentNo, ProgramUitl.COURSE_TYPE ct)
	{
		List<Enroll> elist=getStudentEnrolInfo(studentNo);
		if(elist==null)
		{
			return null;
		}
		else
		{
			List<Enroll> el=new ArrayList<Enroll>();
			for (int i = 0; i < elist.size(); i++) 
			{				
				Enroll eTmp=(Enroll)elist.get(i);
				Course c=searchCourseByNo(Integer.toString(eTmp.getCode()));
				if(c.getType()==ct)
				{
					el.add(eTmp);
				}				
			}
			if(el.size() > 0)
			{
				return el;
			}
			return null;
		}
	}
	
	public ProgramUitl.ERROR_TYPE enroll(String studentNo,List<Enroll> e)
	{
		return enroll(ProgramUitl.ENROLEMENET_DB_PATH+studentNo, studentNo,e);
	}
	
	public ProgramUitl.ERROR_TYPE enroll(String fileName,String studentNo, List<Enroll> e)
	{		
		if(!isFolderAvailable(fileName))
		{							   
				createDir(fileName);
		}
		
		try
		{				
			for (int i = 0; i < e.size(); i++) 
			{				
				Enroll eTmp=(Enroll)e.get(i);
				createXml(fileName+ProgramUitl.DIR_SEP+eTmp.getCode()+ProgramUitl.XML, ProgramUitl.ENROLL_XML_ELEMENT);
				createEnrollFileBody(fileName+ProgramUitl.DIR_SEP+eTmp.getCode()+ProgramUitl.XML, eTmp);
			}			
		} 
		catch (Throwable t) { 
			throw new Error(t);
		} 		
		return ProgramUitl.ERROR_TYPE.ADDED;
	}
		
	public ProgramUitl.ERROR_TYPE enrollStudentByBatch(String studentNo,List<Enroll> e)
	{
		return enrollStudentByBatch(ProgramUitl.ENROLEMENET_DB_PATH+studentNo, studentNo,e);
	}
	
	public ProgramUitl.ERROR_TYPE enrollStudentByBatch(String fileName,String studentNo, List<Enroll> e)
	{	

		if(!isFolderAvailable(fileName))
		{							   
				createDir(fileName);
		}
		
		try
		{		
			
			for (int i = 0; i < e.size(); i++) 
			{				
				Enroll eTmp=(Enroll)e.get(i);				
				if(isFileAvailable(fileName+ProgramUitl.DIR_SEP+eTmp.getCode()+ProgramUitl.XML)==false)
				{
					String fileNameNew=fileName+ProgramUitl.DIR_SEP+eTmp.getCode()+ProgramUitl.XML;
					if(createXml(fileNameNew, ProgramUitl.ENROLL_XML_ELEMENT))
					{						
						createEnrollFileBody(fileNameNew, eTmp);
					}
				}
			}			
		} 
		catch (Throwable t) { 
			throw new Error(t);
		}		
		return ProgramUitl.ERROR_TYPE.ADDED;
	}
	
	public ProgramUitl.ERROR_TYPE enrollBatch(String batchNo,List<Enroll> e)
	{
		return enrollBatch(ProgramUitl.BATCH_DB_PATH, batchNo,e);
	}

	public ProgramUitl.ERROR_TYPE enrollBatch(String fileName,String batchNo, List<Enroll> e)
	{		
		if(!isFolderAvailable(fileName))
		{				
			createDir(fileName);
		}
		
		try
		{					
			//Enroll eTmp=(Enroll)e.get(i);
			createXml(fileName+ProgramUitl.DIR_SEP+batchNo+ProgramUitl.XML, ProgramUitl.ENROLL_XML_ELEMENT_ROOT);			
			createEnrollBatchFileBody(fileName+ProgramUitl.DIR_SEP+batchNo+ProgramUitl.XML, e);	
			//enrollStudentsByBatch(batchNo);			
		} 
		catch (Throwable t) { 
			throw new Error(t);
		} 		
		return ProgramUitl.ERROR_TYPE.ADDED;
	}
	
	public boolean enrollStudentsByBatch(String batchNo)
	{
		List<Student> s =getStudenByBatch(batchNo);
		List<Enroll> e =getBatchEnrolInfo(batchNo);
		
		try
		{
			
			if(s!=null)
			{				
				if(e!=null)
				{
					
					for (int i = 0; i < s.size(); i++) 
					{	
						Student sTmp=(Student)s.get(i);							
						enrollStudentByBatch(Integer.toString(sTmp.getNo()),e);
					}
				}
			
			}
		} 
		catch (Throwable t) { 
			throw new Error(t);
		}
		return true;
	}
	//
	
	//XML File Operations
    private Node getElement(Document doc, Element element, String name, String value, boolean isCdata) 
	{
        Element node = doc.createElement(name);
		if(isCdata==false)
		{
			node.appendChild(doc.createTextNode(value));
		}
		else
		{
			node.appendChild(doc.createCDATASection(value));
		}
        return node;
    }
	
	private boolean isFileAvailable(String fileName)
	{
		File f = new File(fileName);
		return f.exists();
	}
	
	private boolean isFolderAvailable(String fileName)
	{
		File f = new File(fileName);
		return f.isDirectory();
	}

	public boolean createDir(String dirName) {	
		File dir = new File(dirName);
		if (dir.exists()==false) 
		{
			return dir.mkdir();
		}
		return false;
	}
	
	public List<String> getAllFiles(String path,boolean omitExt)
	{
		//List<String> 
		List<String> list=null;
		
		File folder = new File(path);
		File[] fileList = folder.listFiles();
		
		if(fileList.length >0)
		{
			list=new ArrayList<String>();
			for (int i = 0; i < fileList.length; i++) 
			{
				if (fileList[i].isFile()) 
				{
					if(omitExt==true)
					{
						int pos = fileList[i].getName().trim().lastIndexOf(".");
						if (pos > 0) {
							list.add(fileList[i].getName().trim().substring(0, pos));
						}
					}
					else
					{
						list.add(fileList[i].getName().trim().toLowerCase());
					}
				}
			}
		}
		return list;
	}
	
	private boolean createXml(String fileName, String root)
	{
		try
		{
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	 			
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement(root);
			doc.appendChild(rootElement);					
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File(fileName));	 
			transformer.transform(source, result);		
		} 
		catch (Throwable t) { 
			throw new Error(t);
		} 
		return true;
	}
	
	private boolean deleteXml(String fileName)
	{
		File file = new File(fileName);
		return file.delete();
	}
	//
}